package com.school.controller;

import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import com.school.entity.HljtClassimage;
import com.school.entity.HljtSchoolimage;
import com.school.entity.android.Images;
import com.school.entity.web.SchoolImage;
import com.school.service.impl.HljtClassimageServiceImpl;
import com.school.service.impl.HljtSchoolimageServiceImpl;
import com.school.util.JsonInterface;
import com.school.util.Message;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class HljtSchoolimageController {

	@Autowired
	private HljtSchoolimageServiceImpl hljtSchoolimageServiceImpl;
	@Autowired
	private HljtClassimageServiceImpl hljtClassimageServiceImpl;
	
	/**
	 * 安卓视频接口
	 * @param strType
	 * @param req
	 * @param rep
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/selectvideo")
	@ResponseBody
	public JsonInterface selectSchoolImagesByType() throws Exception{
		JsonInterface jsonInterface=new JsonInterface();
		String newIp= WhetherNotToUpdate.getIp();
			List<HljtSchoolimage> Schoolimage=hljtSchoolimageServiceImpl.selectSchoolImageByType("8");
			
			List<Images> imagesList=new ArrayList<>();
			if(null !=Schoolimage){
				for(int i=0;i<Schoolimage.size();i++) {
					Images images=new Images();
					images.setPictureUrl(newIp+Schoolimage.get(i).getImagepath());
					images.setTitle(Schoolimage.get(i).getImagename());
					images.setContent(Schoolimage.get(i).getImagecontent());
					imagesList.add(images);
				}
				JSONArray array=JSONArray.fromObject(imagesList);
				jsonInterface.setData(array.toString());
				jsonInterface.setErrCode(0);
				jsonInterface.setErrMsg("请求成功");
			}else {
				jsonInterface.setData(null);
				jsonInterface.setErrCode(0);
				jsonInterface.setErrMsg("数据有误");
			}
		
		return jsonInterface;
	}
	
	
	/**
	 * 安卓视频接口
	 * @param strType
	 * @param req
	 * @param rep
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/selectvideos")
	@ResponseBody
	public JsonInterface selectSchoolImages() throws Exception{
		JsonInterface jsonInterface=new JsonInterface();
		String newIp= WhetherNotToUpdate.getIp();
		List<HljtSchoolimage> Schoolimage=hljtSchoolimageServiceImpl.selectSchoolImageByType("7");
		
		List<Images> imagesList=new ArrayList<>();
		if(null !=Schoolimage){
			for(int i=0;i<Schoolimage.size();i++) {
				Images images=new Images();
				images.setPictureUrl(newIp+Schoolimage.get(i).getImagepath());
				images.setTitle(Schoolimage.get(i).getImagename());
				images.setContent(Schoolimage.get(i).getImagecontent());
				imagesList.add(images);
			}
			JSONArray array=JSONArray.fromObject(imagesList);
			jsonInterface.setData(array.toString());
			jsonInterface.setErrCode(0);
			jsonInterface.setErrMsg("请求成功");
		}else {
			jsonInterface.setData(null);
			jsonInterface.setErrCode(0);
			jsonInterface.setErrMsg("数据有误");
		}
		return jsonInterface;
	}
	/**
	 * 全屏视频接口
	 * @param classid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selectclassvideos")
	@ResponseBody
	public JsonInterface selectClassImage(Integer classid) throws Exception{
		JsonInterface jsonInterface=new JsonInterface();
		String newIp= WhetherNotToUpdate.getIp();
		if(null !=classid) {
			List<HljtClassimage> Classimage=hljtClassimageServiceImpl.findclassimageall(classid, "7");
			List<Images> imagesList=new ArrayList<>();
			for (int i = 0; i < Classimage.size(); i++) {
				Images images=new Images();
				images.setPictureUrl(newIp+Classimage.get(i).getImagepath());
				images.setTitle(Classimage.get(i).getTitle());
				images.setContent(Classimage.get(i).getContent());
				imagesList.add(images);
			}
			JSONArray array=JSONArray.fromObject(imagesList);
			jsonInterface.setData(array.toString());
			jsonInterface.setErrCode(0);
			jsonInterface.setErrMsg("请求成功");
		}else {
			jsonInterface.setData(null);
			jsonInterface.setErrCode(0);
			jsonInterface.setErrMsg("数据有误");
		}
		
		return jsonInterface;
	}
//=================================================================web接口====================================================================================	
	/**
	 * 学校视频模糊查询
	 * @return
	 * @throws Exception 
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/selectschoolvideo")
	@ResponseBody
	public Message selectschoolvideo(String title,String type,Integer pageSize,Integer  pageIndex,HttpServletResponse rep) throws Exception{
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		if(null !=type) {
			pageIndex=(pageIndex-1)*pageSize;
			List<SchoolImage> SchoolImage=new ArrayList<>();
			if(null !=title) {
				List<HljtSchoolimage> schoolimage=hljtSchoolimageServiceImpl.schoolimageList(type, title);
				List<HljtSchoolimage> Schoolimagepage=hljtSchoolimageServiceImpl.schoolimageListPage(type, title, pageSize, pageIndex);
				for (int i = 0; i < Schoolimagepage.size(); i++) {
					SchoolImage schoolImage=new  SchoolImage();
					schoolImage.setId(Schoolimagepage.get(i).getId());
					schoolImage.setImagename(Schoolimagepage.get(i).getImagename());
					if(Schoolimagepage.get(i).getImagepath().substring(0, 4).equals("http")) {
						schoolImage.setImagepath(Schoolimagepage.get(i).getImagepath());
					}else {
						schoolImage.setImagepath(newIp+Schoolimagepage.get(i).getImagepath());
						
					}
					SchoolImage.add(schoolImage);
				}
				
				data.put("data", SchoolImage);
				data.put("Total", schoolimage.size());
				data.put("success", true);
			}else {
				pageIndex=(pageIndex-1)*pageSize;
				title=null;
				List<HljtSchoolimage> schoolimage=hljtSchoolimageServiceImpl.schoolimageList(type, title);
				List<HljtSchoolimage> Schoolimagepage=hljtSchoolimageServiceImpl.schoolimageListPage(type, title, pageSize, pageIndex);
				for (int i = 0; i < Schoolimagepage.size(); i++) {
					SchoolImage schoolImage=new  SchoolImage();
					schoolImage.setId(Schoolimagepage.get(i).getId());
					schoolImage.setImagename(Schoolimagepage.get(i).getImagename());
					if(Schoolimagepage.get(i).getImagepath().substring(0, 4).equals("http")) {
						schoolImage.setImagepath(Schoolimagepage.get(i).getImagepath());
					}else {
						schoolImage.setImagepath(newIp+Schoolimagepage.get(i).getImagepath());
						
					}
					SchoolImage.add(schoolImage);
				}
				data.put("data", SchoolImage);
				data.put("Total", schoolimage.size());
				data.put("success", true);
			}
			
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
		}
	
	/**
	 * 学校视频添加接口(手动输入)
	 * @param imagename
	 * @param classid
	 * @param imgFile
	 * @param type
	 * @param rep
	 * @return
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/addschoolvideo")
	@ResponseBody
	public Message addschoolvideo(MultipartFile imgFile,String type,String content,HttpServletResponse rep) throws Exception{
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(imgFile.getSize()>0 && null != type ) {
			
			//获取传过来的文件名
		//	String name=FilenameUtils.getBaseName(imgFile.getOriginalFilename());
			//生成新的图片名字
			String name = UUID.randomUUID().toString().replaceAll("-", "");
			//图片后缀名
			String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
			//保存路径D:\java\apache-tomcat-8.0.48\webapps\image\class
			
		//	System.out.println(WhetherNotToUpdate.getSaveDisk()+ name + "." + ext);
			imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk()+ name + "." + ext));
			HljtSchoolimage hljtSchoolimage=new HljtSchoolimage();
			String image=name+"."+ext;
			hljtSchoolimage.setImagepath(WhetherNotToUpdate.getReadDisk()+image);
			
		//	String images=name+".jpg";
		//	System.out.println(WhetherNotToUpdate.getSaveDisk()+images);
		//	WhetherNotToUpdate.fetchFrame(WhetherNotToUpdate.getSaveDisk()+ name + "." + ext,WhetherNotToUpdate.getSaveDisk()+images, 5);
		//	imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk()+images));
			hljtSchoolimage.setImagename(name);
			hljtSchoolimage.setImagecontent(content);
			hljtSchoolimage.setType(type);
			
			hljtSchoolimageServiceImpl.addSchoolImage(hljtSchoolimage);
			
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 
	 * @param type
	 * @param content
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addschoolvideohttp")
	@ResponseBody
	public Message addschoolvideohttp(String type,String title,String content,String imagepath,HttpServletResponse rep) throws Exception{
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=type && null !=content && null !=imagepath) {
			HljtSchoolimage hljtSchoolimage=new HljtSchoolimage();
			hljtSchoolimage.setSchoolid(1);
			hljtSchoolimage.setImagepath(imagepath);
			hljtSchoolimage.setImagename(title);
			hljtSchoolimage.setImagecontent(content);
			hljtSchoolimage.setType(type);
			
			hljtSchoolimageServiceImpl.addSchoolImage(hljtSchoolimage);
			
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 学校视频修改接口
	 * @param id
	 * @param title
	 * @param rep
	 * @return
	 */
	@RequestMapping("/updateschoolvideo")
	@ResponseBody
	public Message updateschoolvideo(Integer id,String title,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			HljtSchoolimage Schoolimage=hljtSchoolimageServiceImpl.findSchoolImage(id);
			
			HljtSchoolimage hljtSchoolimage=new HljtSchoolimage();
			hljtSchoolimage.setId(id);
			hljtSchoolimage.setImagename(title);
			hljtSchoolimage.setImagepath(Schoolimage.getImagepath());
			hljtSchoolimage.setImagecontent(Schoolimage.getImagecontent());
			hljtSchoolimageServiceImpl.updateSchoolImage(hljtSchoolimage);
			
			data.put("success", true);
		}else {
			
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 学校视频删除接口
	 * @param id
	 * @param rep
	 * @return
	 */
	@RequestMapping("/deleteschoolvideo")
	@ResponseBody
	public Message deleteschoolvideo(Integer id,HttpServletResponse rep){
		RepUtil.repPake(rep);
		Message Message=new Message();
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			hljtSchoolimageServiceImpl.deleteSchoolImage(id);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	/**
	 * 学校环境查找
	 * @param pageSize
	 * @param pageIndex
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectschoolscience")
	@ResponseBody
	public Message selectSchoolScience(Integer pageSize,Integer pageIndex,String type,HttpServletResponse rep) throws Exception {
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		String newIp= WhetherNotToUpdate.getIp();
		pageIndex=(pageIndex-1)*pageSize;
		
		List<HljtSchoolimage> schoolimage=hljtSchoolimageServiceImpl.schoolimageList(type, null);
		List<HljtSchoolimage> Schoolimagepage=hljtSchoolimageServiceImpl.schoolimageListPage(type, null, pageSize, pageIndex);
		List<SchoolImage> SchoolImagelist=new ArrayList<>();
		for (int i = 0; i < Schoolimagepage.size(); i++) {
			SchoolImage SchoolImage=new SchoolImage();
			SchoolImage.setId(Schoolimagepage.get(i).getId());
			SchoolImage.setImagename(Schoolimagepage.get(i).getImagename());
			SchoolImage.setImagepath(newIp+Schoolimagepage.get(i).getImagepath());
			SchoolImage.setContent(Schoolimagepage.get(i).getImagecontent());
			SchoolImagelist.add(SchoolImage);
		}
		data.put("data", SchoolImagelist);
		data.put("Total", schoolimage.size());
		data.put("success", true);
		Message.setData(data);
		return Message;
	}
	/**
	 * 修改学校环境
	 * @param id
	 * @param title
	 * @param content
	 * @param imgFile
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateschoolscience")
	@ResponseBody
	public Message updateSchoolScience(Integer id,String title,String content,MultipartFile imgFile,HttpServletResponse rep) throws Exception {
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			HljtSchoolimage hljtschoolimage=new HljtSchoolimage();
			hljtschoolimage.setId(id);
			hljtschoolimage.setImagecontent(content);
			hljtschoolimage.setImagename(title);
			if(imgFile.getSize()>0) {
				//生成新的图片名字
				String name = UUID.randomUUID().toString().replaceAll("-", "");
				//图片后缀名
				String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
				//保存路径D:\java\apache-tomcat-8.0.48\webapps\image\class
				imgFile.transferTo(new File(WhetherNotToUpdate.getSaveDisk() + name + "." + ext));
				String image=WhetherNotToUpdate.getReadDisk()+name+"."+ext;
				hljtschoolimage.setImagepath(image);
			}else {
				HljtSchoolimage HljtSchoolimage=hljtSchoolimageServiceImpl.findSchoolImage(id);
				hljtschoolimage.setImagepath(HljtSchoolimage.getImagepath());
			}
			hljtSchoolimageServiceImpl.updateschoolimage(hljtschoolimage);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 添加学校环境
	 * @param type
	 * @param title
	 * @param content
	 * @param imgFile
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addschoolscience")
	@ResponseBody
	public Message addSchoolScience(String type,String title,String content,MultipartFile imgFile,HttpServletResponse rep) throws Exception {
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		HljtSchoolimage hljtSchoolimage=new  HljtSchoolimage();
		hljtSchoolimage.setImagecontent(content);
		hljtSchoolimage.setImagename(title);
		//生成新的图片名字
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		//图片后缀名
		String ext = FilenameUtils.getExtension(imgFile.getOriginalFilename());
		//保存路径D:\java\apache-tomcat-8.0.48\webapps\image\class
		imgFile.transferTo(new File( WhetherNotToUpdate.getSaveDisk()+"/"+name + "." + ext));
		String image=name+"."+ext;
		hljtSchoolimage.setImagepath(WhetherNotToUpdate.getReadDisk()+image);
		hljtSchoolimage.setType(type);
		
		hljtSchoolimageServiceImpl.addSchoolImage(hljtSchoolimage);
		data.put("success", true);
		Message.setData(data);
		return Message;
	}
	/**
	 * 删除学校环境
	 * @param id
	 * @param rep
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deleteschoolscience")
	@ResponseBody
	public Message deleteSchoolScience(Integer id,HttpServletResponse rep) throws Exception {
		Message Message=new Message();
		RepUtil.repPake(rep);
		Map<String, Object> data =new HashMap<>();
		if(null !=id) {
			hljtSchoolimageServiceImpl.deleteSchoolImage(id);
			data.put("success", true);
		}else {
			data.put("success", false);
		}
		Message.setData(data);
		return Message;
	}
	
	/**
	 * 获取本电脑ip
	 * @return
	 * @throws UnknownHostException
	 */
	 public static final String getIp() throws Exception {
	        String ipString = "";
	        Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
	        InetAddress ip = null;
	        while (allNetInterfaces.hasMoreElements()) {
	            NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
	            Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
	            while (addresses.hasMoreElements()) {
	                ip = (InetAddress) addresses.nextElement();
	                if (ip != null && ip instanceof Inet4Address && !ip.getHostAddress().equals("127.0.0.1")) {
	                    return ip.getHostAddress();
	                }
	            }
	        }
	        return ipString;
	    }
	

	@RequestMapping(value="/selectHljtSchoolimageById")
	@ResponseBody
	public JSONObject selectHljtSchoolimageById(String strID){
		HljtSchoolimage oHljtSchoolimage=new HljtSchoolimage();
		oHljtSchoolimage=hljtSchoolimageServiceImpl.selectHljtSchoolImageById(strID);
		JSONObject myJson=JSONObject.fromObject(oHljtSchoolimage);
		return myJson;

	}
}
